package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSCilent;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class FileController {

    //toDo 上传 下载 删除 文件   以及获取文件url

    /**
     * 文件上传
     *
     * @return 文件上传路径
     */
    @PostMapping
    public String uploadFile(MultipartFile file) throws IOException {

        //封装文件数据
        String name = file.getOriginalFilename();
        byte[] content = file.getBytes();
        String ext = StringUtils.getFilenameExtension(name);
        String author = "作者";
        String md5 = "123456";
        FastDFSFile fastDFSFile = new FastDFSFile(name,content,ext,md5,author);
        //上传文件
        String[] uploads = FastDFSCilent.uploadFile(fastDFSFile);
        String trackerUrl = FastDFSCilent.getTrackerUrl();

        return trackerUrl+"/"+uploads[0]+"/"+uploads[1];
    }


}
